package cn.sh.library.mgts.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sh.library.mgts.bean.ResponseBean;
import cn.sh.library.mgts.bean.SearchMap;
import cn.sh.library.mgts.common.CommonUtil;
import cn.sh.library.mgts.dto.Pager;
import cn.sh.library.mgts.service.HorizonService;

@Controller
@RequestMapping("/sync")
public class SyncEsDataController {


	@Autowired
	HorizonService horizonService;

	@Autowired()@Qualifier("executor")
	ThreadPoolTaskExecutor executor;
	
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/bib")
	public ResponseBean<Object> bib1(@RequestBody SearchMap searchMap) {
//		bulkService.bibData();
		Map<String, Object> resMap = new HashMap<String, Object>();
		List<Map<String, String>> jsonList = new ArrayList<Map<String, String>>();
		Pager pager = new Pager();
		List<Map<String, String>> res = horizonService.searchQuery(searchMap, pager);

		for (int i = 0; i < res.size(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			// 返回map值
			map = res.get(i);
			map.put("key", String.valueOf(i));
			jsonList.add(map);
		}
		resMap.put("pager", pager);
		resMap.put("datas", jsonList);

		return new ResponseBean<Object>(true, resMap);
	}

	
	// 根据bibno获取bib的数据 @PathVariable变量标签
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "{bibno}")
	public Map<String, Object> bibSearch(@PathVariable("bibno") String bibno) {
		String bib = "bib_" + bibno;
		Map<String, Object> res = horizonService.bibQuery(bib);
		return res;
	}
	
	@ResponseBody
	@GetMapping(value = "")
	public ResponseBean<String> sync() {

		return new ResponseBean<String>(true, "true");
	}

	@ResponseBody
	@GetMapping(value = "/item")
	public String item() {
		return "true";
	}

	@ResponseBody
	@GetMapping(value = "/itemNest")
	public String itemNest() {

		return "true";
	}


	@ResponseBody
	@GetMapping(value = "/bib")
	public String bib() {

		return "true";
	}
	
	@ResponseBody
	@GetMapping("/nest")
	public ResponseBean<String> nest(){
		return new ResponseBean<String>(true, "true");
	}
	

	@ResponseBody
	@GetMapping(value = "/scroll")
	public ResponseBean<String> scroll() {
		return new ResponseBean<String>(true, String.valueOf(horizonService.scrolldata()));
	}


	@ResponseBody
	@GetMapping(value = "/bulkAll")
	public ResponseBean<String> bulkAll() {
		return new ResponseBean<String>(true, String.valueOf(1));	
	}
	
	@ResponseBody
	@GetMapping(value = "/bulkUpdate")
	public ResponseBean<String> bulkUpdate() {
		return new ResponseBean<String>(true, String.valueOf(1));	
	}
	
}
